"use strict";

const Controller = require("egg").Controller;

class ShoppingController extends Controller {
    //商品查询
    async inquire() {
        const res = await this.service.shopping.inquire(); //定义查询
        this.ctx.body = {
            code: 200,
            data: res,
            msg: "查询成功",
        };
        return; //返回数据
    }
    //商品添加
    async add() {
        //解析请求体参数
        const { shopname, lettor, price, num } = this.ctx.request.body;
        //判断参数是否为空
        if (!shopname || !lettor) {
            this.ctx.body = {
                code: 401,
                msg: "参数为空",
            };
            return;
        }
        //添加一条商品
        let res = await this.service.shopping.add(shopname, lettor, price, num);
        if (res.affectedRows === 1) {
            this.ctx.body = {
                code: 201,
                msg: "添加成功",
            };
        }
    }
    //删除一个商品
    async remove() {
        // 接收路径参数
        const { id } = this.ctx.query;
        const res = await this.service.shopping.remove(id);
        // 判断是否删除成功
        if (res.affectedRows === 1) {
            this.ctx.body = {
                code: 204,
                msg: "删除成功",
            };
        }
    }
    //商品修改
    async alter() {
        const { num, id } = this.ctx.request.body;
        console.log(num, id);
        const res = await this.service.shopping.alter(num, id);
        this.ctx.body = {
            code: 200,
            msg: "修改成功",
        };
    }
}

module.exports = ShoppingController;
